package com.szdxh.license.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szdxh.license.bo.R;
import com.szdxh.license.dto.AddLicenseDTO;
import com.szdxh.license.dto.SelectPageDTO;
import com.szdxh.license.pojo.License;
import com.szdxh.license.service.impl.LicenseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;

/**
 * @author 双子的小坏
 * @date 2024/7/25 10:09:56
 * @description : 上传视频分析
 */
@RequestMapping("api/license")
@RestController
@RequiredArgsConstructor
public class LicenseController {


    private final LicenseServiceImpl licenseService;

    @PostMapping("/selectList")
    public R selectList(@RequestBody SelectPageDTO selectPageDTO) {
        return new R().success(licenseService.selectPage(selectPageDTO));
    }

    @PostMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody SelectPageDTO selectPageDTO) {
        License temp = new AddLicenseDTO();
        BeanUtil.copyProperties(selectPageDTO, temp);
        if(null != selectPageDTO.getId() && selectPageDTO.getId() != 0) {
            return new R().success(licenseService.updateById(temp));
        } else {
            int insert = licenseService.getBaseMapper().insert(temp);
            return new R().success(temp.getId());
        }
    }

    @GetMapping("/delete")
    public R delete(Long id) {
            return new R().success(licenseService.getBaseMapper().deleteById(id));
    }

    @PostMapping("/select")
    public R select(@RequestBody SelectPageDTO selectPageDTO) {
        QueryWrapper wrapper = new QueryWrapper();
        if(StrUtil.isNotBlank(selectPageDTO.getName())) {
            wrapper.eq("name", selectPageDTO.getName());
        }
        if(StrUtil.isNotBlank(selectPageDTO.getIdCard())) {
            wrapper.eq("id_card", selectPageDTO.getIdCard());
        }
        if(StrUtil.isNotBlank(selectPageDTO.getLicenseNo())) {
            wrapper.eq("license_no", selectPageDTO.getLicenseNo());
        }
        List<License> license = licenseService.getBaseMapper().selectList(wrapper);
        if(license.isEmpty()) {
            return new R().fail();
        }
        return new R().success(license.get(0));
    }
}
